package com.cn.order.service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.cn.dot.AliPayDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Controller
//@RestController//请求方法响应统一json格式
@RequestMapping("/order")
public class PayServiceImpl {

    //应用id
    String APP_ID = "2021000119616506";
    //应用私钥
    String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC1+HwKtGP6DfzT8KX+LLJKcPZXZDsz+UANt3h6MmXBJlM7x6aFY8Sn7mtsChDt7OpzKsDnruta4ziW5Qq7J4Tm3pF57GaOGcNhGdH4AAVPFB5uhDE9mwtBV2bxDP618PAsqCIsMPt/YuKCwr1fQMS+Wr9XxVlO7KP0dJ/Q6Ub+tcW6mcRD+UWxy5h3aqXxnZNz/kyIsO4QlVLY5Jngg+y2/binoc3h4MR81GhGbhBGIsPLwQAyZD//FeuLfmDDT8EgQUqgAOC7dcZENM7NVGcKAteC822Tw/zfgaWgsv1maELxmhcUdNLe9q1DPB+UOOCAPiaCNE3ssqTmp6ZkVtTnAgMBAAECggEAd/l5hvFCTVY1kGLEfl1nu27Y6+t4oR6/nYuOkjpyHjOvguVyI9YQjYvDJXtJiCTAuufAQcBxCf5vjAU82wIGPNUXYyR6xUu1JmsIpZMV7Ppdyg28DVsQw6blhx5DPR+6qia8SRiOaIGAyCevBYNFdB+Oska6SC/7NSxDRlDXbwy6Ou+UxtF2pnEwAYghoLIAjXhvwxQJVWE7zEkdcn2iKwFlEWDfqdy/KGd4twOoUdWEVGEVLo4B5/IwOiUQ8h7XdLFcNl8ILgjzztssb6mg8+AoB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TbWhaILcUla5FAphctUi0SwkQ8nB4ZZ/StqaBT0ed1AV+0MO1Eg6ll8mwNTgvEkLMyQlsizWx1FevNk4XT6s44GJay7OmnzPJxf6jkaRjl4yJipWkLqLeDb+prpQwJQFeh8Vyk39MRFbYNPNqMj7t9/rOH83DXMwXab2mkiqE9dAUfAx4udMKdH/6fjr8ZE8i5VpXpjjAmuofg0iLGz8TJIVROMFXoR5/pWNg5pt5t8Lany2uC/L7zJGpcKDQjfXiY8RdXSLzb5HI3OdPov2fwIDAQAB";

    String CHARSET = "utf-8";
    //支付宝接口的网关地址，正式"https://openapi.alipay.com/gateway.do"
    String serverUrl = "https://openapi.alipaydev.com/gateway.do";
    //签名算法类型
    String sign_type = "RSA2";

    @GetMapping("/alipaytest")
    public void alipaytest(HttpServletRequest httpRequest,
                           HttpServletResponse httpResponse,String traceNo,String totalAmount) throws ServletException, IOException {
        //构造sdk的客户端对象
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, sign_type); //获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
//        alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
        alipayRequest.setNotifyUrl("http://ibkjgk.natappfree.cc/api/order/notify");//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                " \"out_trade_no\":\"" + traceNo+
                "\", \"total_amount\":\"" + totalAmount+
                "\",\"subject\":\"景区门票\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");//填充业务参数
        String form="";
        try {
            //请求支付宝下单接口,发起http请求
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }

}

